<template>
  <div>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          fixed
          prop="id"
          label="id"
          width="150">
        </el-table-column>
        <el-table-column
          prop="name"
          label="书名"
          width="120">
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          width="120">
        </el-table-column>
        <el-table-column
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="deleteBook(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="total"
      @current-change="mypage">
    </el-pagination>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  methods: {
    handleClick (row) {
      console.log(row)
      // this.$router.push('/UpdateBook') // 这只能跳到UpdateBook,不能带数据过去
      this.$router.push({ // 这样写就可以把编号带到UpdateBook中
        path: '/UpdateBook',
        query: {
          id: row.id
        }
      })
    },
    mypage (currentpage) {
      const _this = this
      axios.get('http://localhost:8181/book/findAll/' + (currentpage - 1) + '/2').then(function (resp) {
        _this.tableData = resp.data.content
        _this.pageSize = resp.data.size
        _this.total = resp.data.totalElements
      })
    },
    deleteBook (row) {
      const _this = this
      axios.delete('http://localhost:8181/book/delete/' + row.id).then(function (resp) {
        _this.$alert('《' + row.name + '》删除成功！', '消息', {
          confirmButtonText: '确定',
          callback: action => {
            // _this.$router.push('/BookManage') /* 跳转到BookManage页面 */
            window.location.reload()
          }
        })
      })
    }
  },
  created () {
    const _this = this
    axios.get('http://localhost:8181/book/findAll/0/2').then(function (resp) {
      console.log(resp)
      _this.tableData = resp.data.content
      _this.pageSize = resp.data.size
      _this.total = resp.data.totalElements
    })
  },

  data () {
    return {
      pageSize: '1',
      total: '2',
      tableData: [{
        id: '1',
        name: '西游记',
        author: '张三1'

      }, {
        id: '2',
        name: '西游记2',
        author: '张三2'
      }, {
        id: '3',
        name: '西游记3',
        author: '张三3'
      }, {
        id: '4',
        name: '西游记4',
        author: '张三4'
      }]
    }
  }
}
</script>
